#coding: utf-8
class Report::CollectionsReportController < ApplicationController
  def show_pdf

    @cultural_heritage_collection = CulturalHeritage::Collection.find(params[:id])

    @cultural_heritage_collection_photos = @cultural_heritage_collection.list_photos

    pdf = Prawn::Document.new

    pdf.image "#{Rails.root}/public/images/pukara_logo.png", :width => 80, :at => [0, 715]
    pdf.text_box "Sistema de Información Cultural Pukara",
      :at => [100, 715],
      :width => 430
    #    Titulo del reporte
    pdf.text_box "Detalle de Colección "+@cultural_heritage_collection.name,
      :at => [100, 690],
      :width => 430,
      :style => :bold,
      :size => 20
    #    Cuerpo

    pdf.move_down 150

    datos = Array.new

    #Colocando labels
    datos[0] = Array.new
    datos[1] = Array.new
    datos[2] = Array.new
    datos[3] = Array.new
    datos[4] = Array.new
    datos[5] = Array.new

    datos[0][0] = "Descripción: "
    datos[1][0] = "Tamaño original en unidades: "
    datos[2][0] = "Tamaño actual en unidades: "

    datos[0][1] = @cultural_heritage_collection.description
    datos[1][1] = @cultural_heritage_collection.original_size.to_s
    datos[2][1] = @cultural_heritage_collection.current_size.to_s

    if @cultural_heritage_collection.str_owner
      datos[3][0] = "Propietario: "
      datos[3][1] = @cultural_heritage_collection.str_owner
    end

    if @cultural_heritage_collection.str_state
      datos[4][0] = "Estado: "
      datos[4][1] = @cultural_heritage_collection.str_state
    end

    if @cultural_heritage_collection.cultural_institution
      datos[5][0] = "Institución Cultural: "
      datos[5][1] = @cultural_heritage_collection.cultural_institution
    end

    pdf.table datos, :column_widths => [210, 330], :row_colors => ["F0F0F0", "FFFFFF"] do
        #row(0).border_width = 0
        column(0).font_style = :bold
    end



#    pdf.text_box "Descripción: ",
#      :style => :bold,
#      :at => [100, 560]
#    pdf.text_box @cultural_heritage_collection.description,
#      :at => [100, 550]
#
#
#    pdf.text_box "Tamaño original en unidades: ",
#      :style => :bold,
#      :at => [100, 530]
#    pdf.text_box @cultural_heritage_collection.original_size.to_s,
#      :at => [100, 520]
#
#
#    pdf.text_box "Tamaño actual en unidades: ",
#      :style => :bold,
#      :at => [100, 500]
#    pdf.text_box @cultural_heritage_collection.current_size.to_s,
#      :at => [100, 490]
#
#
#    pdf.text_box "Propietario: ",
#      :style => :bold,
#      :at => [100, 470]
#    pdf.text_box @cultural_heritage_collection.str_owner,
#      :at => [100, 460]
#
#
#    if @cultural_heritage_collection.str_state
#      pdf.text_box "Estado: ",
#        :style => :bold,
#        :at => [100, 450]
#      pdf.text_box @cultural_heritage_collection.str_state,
#        :at => [100, 440]
#    end
#
#    if @cultural_heritage_collection.cultural_institution
#      pdf.text_box "Institución Cultural: ",
#        :style => :bold,
#      :at => [100, 420]
#      pdf.text_box @cultural_heritage_collection.cultural_institution.to_s,
#        :at => [100, 410]
#    end

    pdf.move_down 150

    if @cultural_heritage_collection_photos.size > 0
      photo_rows = @cultural_heritage_collection_photos.size/5 + 1

      pdf.define_grid(:columns => 5, :rows => photo_rows, :gutter => 10)
      pdf.start_new_page
      #pdf.grid.show_all

      #Galería
      pdf.text_box "Galería: ",
          :style => :bold
          #:at => [100, 450]

#      pdf.move_down 35

      i_photo = 0
    
      pdf.grid.rows.times do |i|
        pdf.grid.columns.times do |j|
          b = pdf.grid(i,j)
          pdf.grid(i,j).bounding_box do#b.top_left, :width => b.width, :height => b.height do

              if (i_photo < @cultural_heritage_collection_photos.size)
                cultural_heritage_collection_photo = @cultural_heritage_collection_photos[i_photo]
                
  #              pdf.rectangle(pdf.bounds.top_left, b.width, b.height)
                if ( !(cultural_heritage_collection_photo.photo.url.blank? || cultural_heritage_collection_photo.photo_file_name.blank?) &&
                  ((cultural_heritage_collection_photo.photo_file_name.include?('.jpg')) || (cultural_heritage_collection_photo.photo_file_name.include?('.png')))
                )
                  pdf.image ("#{Rails.root}" << "/public" << cultural_heritage_collection_photo.photo.url).split("?").at(0), :width => b.width
                else
                  pdf.image "#{Rails.root}/public/images/pukara_logo.png", :height => b.height, :width => b.width
                end
                i_photo = i_photo + 1
              end
          end
        end
      end

#      @cultural_heritage_collection_photos.each do |cultural_heritage_collection_photo|
#        if !(cultural_heritage_collection_photo.photo.url.blank? || cultural_heritage_collection_photo.photo_file_name.blank?)
#          #solo aguanta 2 formatos
#          if ((cultural_heritage_collection_photo.photo_file_name.include?('.jpg')) || (cultural_heritage_collection_photo.photo_file_name.include?('.png')))
#            pdf.grid([a,b],[a,b]).bounding_box do
#              pdf.image ("#{Rails.root}" << "/public" << cultural_heritage_collection_photo.photo.url).split("?").at(0), :height => 100, :width => 100#:position => :center#, :vposition => 130
#              pdf.text a.to_s
#              pdf.text b.to_s
#            end
#            a = a + 1
#            if b == 4
#              b = 0
#            else
#              b = b + 1
#            end
#          else
#            pdf.grid([a,b],[a,b]).bounding_box do
#              pdf.image "#{Rails.root}/public/images/pukara_logo.png", :height => 100, :width => 100#:position => :center, :vposition => 130
#              pdf.text a.to_s
#              pdf.text b.to_s
#            end
#            a = a + 1
#            if b == 4
#              b = 0
#            else
#              b = b + 1
#            end
#          end
#        else
#          pdf.grid([a,b],[a,b]).bounding_box do
#            pdf.image "#{Rails.root}/public/images/pukara_logo.png", :height => 100, :width => 100#:position => :center, :vposition => 130
#            pdf.text a.to_s
#            pdf.text b.to_s
#          end
#          a = a + 1
#          if b == 4
#            b = 0
#          else
#            b = b + 1
#          end
#        end
#      end

    end

    pdf.text_box "Galería: ",
          :style => :bold
        

    pdf.start_new_page
    #miembros de la colección
    pdf.text_box "Miembros: ",
        :style => :bold

    pdf.move_down 35

    if @cultural_heritage_collection.cultural_heritages.size > 0

      arr = Array.new

      #Colocando headers
      arr[0] = Array.new
      arr[0][0] = "Código RN"
      arr[0][1] = "Nombre"
      arr[0][2] = "Descripción"
      arr[0][3] = "Foto"

      j = 1


      @cultural_heritage_collection.cultural_heritages.each do |cultural_heritage|
        arr[j] = Array.new
        arr[j][0] = cultural_heritage.id
        arr[j][1] = cultural_heritage.name
        arr[j][2] = cultural_heritage.description
        
        #La fotito
        if !(cultural_heritage.avatar.url.blank? || cultural_heritage.avatar_file_name.blank?)
          if ((cultural_heritage.avatar_file_name.include?('.jpg')) || (cultural_heritage.avatar_file_name.include?('.png')))
            arr[j][3] =  ("#{Rails.root}" << "/public" << cultural_heritage.avatar.url).split("?").at(0)
          else
            arr[j][3] = "#{Rails.root}/public/images/pukara_logo.png"
          end
        else
          arr[j][3] = "#{Rails.root}/public/images/pukara_logo.png"
        end

        j = j + 1
      end

#      k = 1
      pdf.table arr, :column_widths => [60, 150, 220 , 110], :row_colors => ["F0F0F0", "FFFFFF"], :header => true do
        row(0).border_width = 2
        row(0).font_style = :bold

#        row(k).height = 100
#
#        k = k + 1
      end
      #fin table

      pdf.text_box "Miembros: ",
        :style => :bold

    else
      pdf.text_box "La colección está vacía."

    end

    #Pie de pagina
    pdf.repeat(:all, :dynamic => true) do
      pdf.draw_text Time.now.strftime("%d-%m-%Y").to_s, :at => [0, 0]
      pdf.draw_text pdf.page_number, :at => [530, 0]
    end
    #Nombre de reporte
    send_data pdf.render, :filename => "reporte_colección_"+@cultural_heritage_collection.id.to_s << ".pdf", :type => "application/pdf"
  end
end
